package com.projectsexception.festivales.setup.xml;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.projectsexception.festivales.setup.MapInfo;

public class MapInfoHandler extends DefaultHandler {
    
    private List<MapInfo> maps;
    private MapInfo tmpMapInfo;
    private boolean mapURL;

    @Override
    public void startDocument() {
        maps = new ArrayList<MapInfo>();
        mapURL = false;
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if(mapURL) {
            tmpMapInfo.setImageURL(new String(ch,start,length));
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        String s = SAXHandlerUtil.getElementName(localName, qName);
        if (s.equals("map")) {
            tmpMapInfo = new MapInfo();
            tmpMapInfo.setName(attributes.getValue("name"));
        } else if (s.equals("imageURL")) {
            mapURL = true;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        String s = SAXHandlerUtil.getElementName(localName, qName);
        if ("map".equals(s)) {
            maps.add(tmpMapInfo);
        } else if (s.equals("imageURL")) {
            mapURL = false;
        }
    }

    public List<MapInfo> getResult() {
        return maps;
    }

}
